import React, {Component} from "react";
import {
    Navigator,
    AsyncStorage,
    StyleSheet,
    View,
    Platform,
    BackAndroid,
    StatusBar
} from "react-native";
import Splash from "./Splash";
import Storage from "react-native-storage";
import Global from "./Model/Global/GCPublic";
import GCStorage from "./Model/DataTool/GCStorage";
let defaultComponent = Splash;

export default class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {};

    }

    componentDidMount() {

        GCStorage.read(
            Global.Storage.storageUser
        ).then((ret) => {
            Global.User.id = ret.id;
            Global.User.token = ret.token;
            Global.User.name = ret.name;
            Global.User.mobile = ret.mobile;
            Global.User.business_licence = ret.business_licence;
            Global.User.certificate = ret.certificate;
            Global.User.corporate_bankno = ret.corporate_bankno;
        }).catch((err) => {
        });

        if (Platform.OS === 'android') {
            BackAndroid.addEventListener('hardwareBackPress', this.onBackAndroid);
        }

        if (Platform.OS === 'android') {
            BackAndroid.addEventListener('hardwareBackPress', this.onBackAndroid);
        }
    }

    componentWillUnmount() {

        if (Platform.OS === 'android') {
            BackAndroid.removeEventListener('hardwareBackPress', this.onBackAndroid)
        }

    }

    onBackAndroid = () => {
        const nav = this.refs['navigator'];
        if (nav === undefined) return false;
        const routers = nav.getCurrentRoutes();
        if (routers.length > 1) {
            nav.pop();
            return true;
        }
        return false;
    };

    /**
     * 配置场景动画
     * @param route 路由
     * @param routeStack 路由栈
     * @returns {*} 动画
     */
    configureScene(route, routeStack) {
        if (route.type == 'Bottom') {
            return Navigator.SceneConfigs.FloatFromBottom; // 底部弹出
        } else if (route.type == 'FromLeft') {
            return Navigator.SceneConfigs.FloatFromLeft;
        } else if (route.type == 'FromRight') {
            return Navigator.SceneConfigs.FloatFromRight;
        } else {
            return Navigator.SceneConfigs.PushFromRight;// 右侧弹出
        }
    }

    render() {
        // let defaultName = '';
        return (
            <View style={{flex: 1}}>
                <StatusBar backgroundColor="#22cb86" barStyle="light-content" />
                <Navigator
                    ref="navigator"
                    initialRoute={{name: '', component: defaultComponent}}
                    configureScene={this.configureScene}
                    renderScene={(route, navigator) => {
                        let Component = route.component;
                        return <Component {...route.params} navigator={navigator}/>
                    }}>

                </Navigator>
            </View>

        );
    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',


    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
    },
});

// AppRegistry.registerComponent('DPProject', () => App);
